<?php

class SoftDeletableBehavior extends ModelBehavior {
	protected $__config = array();

	public function setup(Model $Model, $config = array()) {
		$default = array(
			'delete' => true,
			'find' => true
		);

		if (!isset($this->__config[$Model->alias])) {
			$this->__config[$Model->alias] = $default;
		}

		if (is_array($config)) {
			$this->__config[$Model->alias] = array_merge(
				$this->__config[$Model->alias],
				$config
			);
		}
	}

	public function beforeDelete(Model $Model, $cascade = true) {
		$id = $Model->id;
		$deleted = $Model->saveField('deleted', 1, false);

// TODO FIX cascading so _deleteDependent doesn't result in a SQL Error 42000/1064
// 		if ($deleted && $cascade) {
// 			$Model->_deleteDependent($id, $cascade);
// 			$Model->_deleteLinks($id);
// 			return false;
// 		}
		return true;
	}

/* Commented out because not necessary
	public function hardDelete(Model $Model, $id, $cascade = true) {
		$onFind = $this->__config[$Model->alias]['find'];
		$onDelete = $this->__config[$Model->alias]['delete'];
		$this->enableSoftDeletable($Model, false);

		$deleted = $Model->del($id, $cascade);

		$this->enableSoftDeletable($Model, 'delete', $onDelete);
		$this->enableSoftDeletable($Model, 'find', $onFind);

		return $deleted;
	}
 */

/* Commented out because not necessary
	public function purge(Model $Model, $cascade = true) {
		$purged = false;

		$onFind = $this->__config[$Model->alias]['find'];
		$onDelete = $this->__config[$Model->alias]['delete'];
		$this->enableSoftDeletable($Model, false);

		$purged = $Model->deleteAll(array('deleted' => '1'), $cascade);

		$this->enableSoftDeletable($Model, 'delete', $onDelete);
		$this->enableSoftDeletable($Model, 'find', $onFind);

		return $purged;
	}
*/

	public function undelete(Model $Model, $id = null) {
		if (empty($id)) {
			$id = $Model->id;
		}

		$data = array($Model->alias => array(
				$Model->primaryKey => $id,
				$this->deleted => '0'
		));

		$onFind = $this->__config[$Model->alias]['find'];
		$onDelete = $this->__config[$Model->alias]['delete'];
		$this->enableSoftDeletable($Model, false);

		$Model->id = $id;
		$result = $Model->save($data, false, array_keys($data[$Model->alias]));

		$this->enableSoftDeletable($Model, 'find', $onFind);
		$this->enableSoftDeletable($Model, 'delete', $onDelete);

		return ($result !== false);
	}

	public function enableSoftDeletable(Model $Model, $methods, $enable = true) {
		if (is_bool($methods)) {
			$enable = $methods;
			$methods = array('find', 'delete');
		}

		if (!is_array($methods)) {
			$methods = array($methods);
		}

		foreach($methods as $method) {
			$this->__config[$Model->alias][$method] = $enable;
		}
	}

	public function beforeFind(Model $Model, $queryData) {
		$key = $Model->alias . '.deleted';
		$queryData['conditions'][$key] = 0;
		return $queryData;
	}

/* Commented out because not necessary
	public function beforeSave(Model $Model) {
		if ($this->__config[$Model->alias]['find']) {
			if (!isset($this->__backAttributes)) {
				$this->__backAttributes = array($Model->alias => array());
			} else if (!isset($this->__backAttributes[$Model->alias])) {
				$this->__backAttributes[$Model->alias] = array();
			}
			$this->__backAttributes[$Model->alias]['find'] = $this->__config[$Model->alias]['find'];
			$this->__backAttributes[$Model->alias]['delete'] = $this->__config[$Model->alias]['delete'];
			$this->enableSoftDeletable($Model, false);
		}
		return true;
	}
*/

/* Commented out because not necessary
	public function afterSave(Model $Model, $created) {
		if (isset($this->__backAttributes[$Model->alias]['find'])) {
			$this->enableSoftDeletable($Model, 'find', $this->__backAttributes[$Model->alias]['find']);
			$this->enableSoftDeletable($Model, 'delete', $this->__backAttributes[$Model->alias]['delete']);
			unset($this->__backAttributes[$Model->alias]['find']);
			unset($this->__backAttributes[$Model->alias]['delete']);
		}
	}
*/
}
